#include<iostream>

using namespace std;

struct ListNode{
    int value;
    ListNode * next;
};

// 尾插
ListNode * createNode(int * data, int length){
    if ( data == nullptr || length <= 0 )
        return nullptr;
    ListNode * head = new ListNode();
    head -> value = data[0];
    head -> next = nullptr;
    ListNode * pHead = head;
    for ( int i=1;i<length; i++ ){
        ListNode * node = new ListNode();
        node -> value = data[i];
        node -> next = nullptr;
        pHead = pHead -> next = node;
    }
    return head;
}

// 翻转链表： 前插节点
ListNode * reverseList(ListNode * head){
    if ( head == nullptr )
        return nullptr;

    ListNode *pHead = nullptr;
    ListNode * node = head;

    while ( node != nullptr ){
	    // 注意前插和尾插的区别
        ListNode * reverseNode = node;
	    node = node -> next;
        reverseNode -> next = pHead;
	    pHead = reverseNode;
    } 
    return pHead;
}

void printNode(ListNode * head){
    ListNode * node = head;
    while ( node != nullptr ){
        cout << node -> value << endl;
        node = node -> next;
    }
}

int main(void){
    int data[] = {1,2,3,4,5,6,7,8,9,0};
    ListNode * head = createNode(data, sizeof(data)/sizeof(int));
if ( head == nullptr ){
	    cout << "input param is invaild" << endl;
    }else{
        printNode(head);
    } 
cout << "-------" << endl;
    head = reverseList(head);

    if ( head == nullptr ){
	    cout << "input param is invaild" << endl;
    }else{
        printNode(head);
    }    
    return 0;
}